<html>

<head>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
  <!-- <script type="text/javascript" src="js/dom-to-image.js"></script> -->
  <script src="https://cdn.bootcdn.net/ajax/libs/dom-to-image/2.6.0/dom-to-image.js"></script>
  <!-- <script type="text/javascript" src="js/FileSaver.js"></script> -->
  <script src="https://cdn.bootcdn.net/ajax/libs/FileSaver.js/2.0.5/FileSaver.js"></script>

  <script type="text/javascript">
    $(function () {
      //
      $("#btn").click(function () {
        //插入图片到html
        //   var node = document.getElementById('table');
        //       domtoimage.toPng(node)
        //                 .then(function (dataUrl) {
        //                     console.log(dataUrl)
        //                     var img = new Image();
        //                     img.src = dataUrl;
        //                     document.body.appendChild(img);
        //                 });


        // 下载png图片
        /* domtoimage.toBlob(document.getElementById('table'))
            .then(function (blob) {
                console.log(blob)
                window.saveAs(blob, 'my-node.png');
            }); */


        //   下载jpeg图片
        domtoimage.toJpeg(document.getElementById('table'), { quality: 0.95 })
          .then(function (dataUrl) {
            var link = document.createElement('a');
            link.download = 'my-image-name.jpeg';
            link.href = dataUrl;
            link.click();
          });

        // var node = document.getElementById('table');
        // domtoimage.toPixelData(node)
        //     .then(function (pixels) {
        //         for (var y = 0; y < node.scrollHeight; ++y) {
        //           for (var x = 0; x < node.scrollWidth; ++x) {
        //             pixelAtXYOffset = (4 * y * node.scrollHeight) + (4 * x);
        //             /* pixelAtXY is a Uint8Array[4] containing RGBA values of the pixel at (x, y) in the range 0..255 */
        //             pixelAtXY = pixels.slice(pixelAtXYOffset, pixelAtXYOffset + 4);
        //           }
        //         }
        //     });    
      })
    })
  </script>
</head>

<body>
  <input type="button" id="btn" value="do it" />
  <div id="my-node">123</div>
  <h4>两行三列：</h4>
  <table id="table" border="1" style="width:200px;border-collapse: collapse;border:1px solid black;">
    <tr style="background:green;">
      <td rowspan="3">200</td>
      <td>200</td>
      <td>300</td>
    </tr>
    <tr style="background:green;">
      <td rowspan="2">500</td>
      <td>600</td>
    </tr>
    <tr style="background:green;">
      <td>600</td>
    </tr>
    <tr style="background:skyblue;">
      <td rowspan="3">100</td>
      <td>200</td>
      <td>300</td>
    </tr>
    <tr style="background:skyblue;">
      <td>500</td>
      <td>600</td>
    </tr>
    <tr style="background:skyblue;">
      <td>600</td>
      <td>600</td>
    </tr>
  </table>
</body>

</html>